package com.atguigu.cloud.sevice.Impl;/**
 * @Auter zzh
 * @Date 2024/11/13
 */

import com.atguigu.cloud.entities.Admin;
import com.atguigu.cloud.entities.Merchant;
import com.atguigu.cloud.sevice.MerchantService;
import com.atguigu.cloud.tool.load.page.PaginationResult;
import com.atguigu.cloud.tool.load.page.QueryObject.BaseQueryObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.atguigu.cloud.mapper.MerchantMapper;

import java.util.Collections;
import java.util.List;

/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.sevice.Impl
 * @className: merchantServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/13 17:58
 * @version: 1.0
 */
@Service
public class MerchantServiceImpl implements MerchantService {
    @Resource
    MerchantMapper merchantMapper;



    @Override
    //添加商人
    public int add(Merchant merchant) {
        merchantMapper.insert(merchant);
        return merchant.getId();
    }

    @Override
    //删除商人
    public int delete(Integer id) {
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int result_id =merchantMapper.delete(queryWrapper);
        return result_id;

    }

    @Override
    //更新商人信息
    public int update(Merchant merchant) {
        int id = merchant.getId();
        UpdateWrapper<Merchant> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        merchantMapper.update(merchant,updateWrapper);
        return id;

    }

    @Override
    public boolean onlyName(String name) {
            QueryWrapper<Merchant> querywrapper = new QueryWrapper<>();
            querywrapper.eq("name",name);
            return merchantMapper.selectList(querywrapper).isEmpty();
        }

    @Override
    public Integer getIdByName(String name) {
        QueryWrapper<Merchant> querywrapper = new QueryWrapper<>();
        querywrapper.eq("name",name).select("id");
        return  merchantMapper.selectOne(querywrapper).getId();
    }


    @Override
    //获取商人信息根据id
    public Merchant getById(Integer id) {
        QueryWrapper<Merchant> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Merchant merchant = merchantMapper.selectOne(queryWrapper);
        return merchant;

    }

    @Override
    //获取所有的商人信息
    public List<Merchant> getAll() {
        QueryWrapper<Merchant> queryWrapper= new QueryWrapper<>();
        List<Merchant> list_merchant = merchantMapper.selectList(queryWrapper);
        return list_merchant;
    }


    @Override
    public PaginationResult<Merchant> myPaginationQuery(Integer currentPage) {
        //为0排除
        if (currentPage==0){
            throw new RuntimeException("输入目标页号为0,这将不被允许");
        }
        //分页对象,包含分页大小和分页号数
        BaseQueryObject qo =new BaseQueryObject(currentPage);

        QueryWrapper<Merchant> queryWrapper=new QueryWrapper<>();

        // 获取查询的记录数
        long totalCount = merchantMapper.selectCount(queryWrapper);
        // 获得查询的目标结果集,(这里就用基础的mybatis实现了)
        List<Merchant> list_merchant = merchantMapper.PaginationQuery(qo.getStart(),qo.getPageSize());

        // 如果总记录数为0，那么说明没数据就不用下一步查询了，提高效率。
        if (totalCount == 0){
            // 返回一个查询结果集，返回当前页、每页记录数、以及一个空的结果集
            return new PaginationResult<Merchant>(qo.getCurrentPage(),qo.getPageSize(),totalCount, Collections.EMPTY_LIST);
        }

        // 返回一个查询结果集，返回当前页、每页记录数、以及结果集
        return new PaginationResult<Merchant>(qo.getCurrentPage(),qo.getPageSize(),totalCount,list_merchant);
    }
}
